/**
 *  蓝牙初始化、开、锁指令
 *  开关锁首先发送一次查询锁状态指令，查看当前锁舌以及锁舌的状态
 */
import store from '@/store'
export default {
	data() {
		return {
			option: null,
			timer: null,
			keep: false
		}
	},
	methods: {
		connectionAndOpen(obj) {
			let {
				params
			} = obj
			this.option = {
				...params
			}
			this.option.type = 'open'
			this.beforeInitBlueAndConnection(obj)
		},
		connectionAndClose(obj) {
			let {
				params
			} = obj
			this.option = {
				...params
			}
			this.option.type = 'close'
			this.beforeInitBlueAndConnection(obj)
		},
		beforeInitBlueAndConnection(obj) {
			let that = this
			wx.getSetting({
				success(res) {
					//判断是否有'scope.bluetooth'属性
					if (res.authSetting.hasOwnProperty('scope.bluetooth')) {
						//'scope.bluetooth'属性存在，且为false
						if (!res.authSetting['scope.bluetooth']) {
							//弹窗授权
							wx.openSetting({
								success(res) {
									that.initBlueAndConnection(obj)
								}
							})
						} else {
							that.initBlueAndConnection(obj)
						}
					} else
						//'scope.bluetooth'属性不存在，需要授权
						wx.authorize({
							scope: 'scope.bluetooth',
							success() {
								// 用户已经同意小程序使用手机蓝牙功能，后续调用 蓝牙 接口不会弹窗询问
								that.initBlueAndConnection(obj)
							}
						})

				}
			})
		},
		keepLink() {
			if (this.keep) {
				let bt = store.state.bluetooth.bluetooth
				let str = `3c4c696e6b${this.intTohex(250)}3e`
				bt.send(str)
				this.timer = setTimeout(() => {
					this.keepLink()
				}, 3000)
			} else {
				this.clearTimer()
			}
		},
		clearTimer() {
			clearTimeout(this.timer)
			this.timer = null
		},
		initBlueAndConnection({
			params,
			success,
			fail
		} = {}) {
			wx.showLoading({
				title: '连接中',
				mask: true
			})
			let that = this
			let bt = store.state.bluetooth.bluetooth
			bt.assign({
				...this.option,
				success,
				fail,
				ready: () => {
					if (this.option.type == 'open') {
						wx.hideLoading()
						that.keep = true
						// that.keepLink()
						that.openBtlock()
						// wx.showModal({
						// 	title: '请输入时间',
						// 	editable: true,
						// 	confirmText: '开锁',
						// 	success(res) {
						// 		if (res.confirm) {
						// 			if (!res.content) {
						// 				res.content = 1
						// 			}
						// 			this.option.time = res.content
						// 			that.keep = false
						// 			that.openBtlock()
						// 		}
						// 	}
						// })
					}
					if (this.option.type == 'close') {
						that.closeBtlock()
					}
				}
			}).init()

		},
		closeBluetooth() {
			this.clearTimer()
			let bt = store.state.bluetooth.bluetooth
			bt.close()
		},
		intTohex(time) {
			let t = Number(time)
			let ret = t.toString(16)
			if (ret.length > 1) {
				return ret
			} else {
				return '0' + ret
			}
		},
		// 开锁
		openBtlock({
			params,
			success,
			fail
		} = {}) {
			let bt = store.state.bluetooth.bluetooth
			let str = `3c4c696e6b${this.intTohex(this.option.time)}3e`
			bt.sendData(str, res => {
				if (res == `3c426c6573${this.intTohex(this.option.time)}3e`) {
					bt.options.success && bt.options.success(res)
				}
			})
		},
		// 关锁
		closeBtlock({
			params,
			success,
			fail
		} = {}) {
			let bt = store.state.bluetooth.bluetooth
			let str = `3c4c696e6b003e`
			bt.sendData(str, res => {
				
				wx.hideLoading()
				if (res.includes('3c426c6573003e')) {
					bt.options.success && bt.options.success(res)
				}
			})

		},
	}
}
